package org.scalacn.easyscala.snippet

import _root_.scala.xml.{NodeSeq, Text}
import _root_.net.liftweb.util._
import Helpers._
import _root_.net.liftweb.common._
import _root_.net.liftweb.http._
import SHtml._
import _root_.net.liftweb.http.js._
import JE._
import JsCmds._

import org.scalacn.easyscala.interpreter.ScalaInterpreter

class SI {
  def ip(in: NodeSeq): NodeSeq = {
	def interp(script: String): JsCmd = {
	  SI.printMemory("SI>ip>before interp")
	  val res: String = SI.interpreter.execute(script)
	  //update result
	  JsFunc("updateScalaResultTable", JsObj(("text",res)), "scalaresult").cmd 
	} 
    
    def doBind(form: NodeSeq) = {
      SI.printMemory("SI>ip>before doBind")
      bind("edit", form,
      "script" -> textarea("println(\"Hello EasyScala\")", interp,("rows","3"),("id","inputscript"),("class","input")) 
     )
	}
    SI.printMemory("SI>ip>after doBind")
   doBind(in)
}
}

object SI {
  	def printMemory(msg: String){
		  val freeMemory: Long = Runtime.getRuntime().freeMemory()
		  val totalMemory: Long = Runtime.getRuntime().totalMemory()
		  val maxMemory: Long = Runtime.getRuntime().maxMemory()
 
		  val memoryUsed: Long = totalMemory-freeMemory

		System.out.println(msg + " memoryUsed: " + memoryUsed/1024/1024+"M")
	}
   
  	val interpreter = new ScalaInterpreter()
}
